package nf.util;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

public abstract class MappedCollection<O, D> extends AbstractCollection<D>
{
	private Collection<O> originalCollection;
	
	public MappedCollection(Collection<O> original)
	{
		originalCollection = original;
	}
	
	public int size()
	{
		return originalCollection.size();
	}
	
	public Iterator<D> iterator()
	{
		return new MappedIterator();
	}
	
	protected abstract D translate(O orig);
	
	private class MappedIterator implements Iterator<D>
	{
		private Iterator<O> orig;
		
		public MappedIterator()
		{
			orig = originalCollection.iterator();
		}
		
		public boolean hasNext()
		{
			return orig.hasNext();
		}
		
		public D next() throws NoSuchElementException
		{
			return translate(orig.next());
		}
		
		public void remove()
		{
			throw new UnsupportedOperationException();
		}
	}
}
